import { Component, OnInit } from '@angular/core';
import * as ons from 'onsenui';
import _contentReady from 'onsenui/esm/ons/content-ready';
import { Router } from '@angular/router';
import { CarService } from '../car.service';

@Component({
  selector: 'app-add-car',
  templateUrl: './add-car.component.html',
  styleUrls: ['./add-car.component.less']
})

export class AddCarComponent implements OnInit {

  cardNo: string = ''
  vin: string = ''
  cardNo2: string = ''
  type: string = '02'
  note: string = ''
  check: boolean = true
  province: string = '浙';
  modifiers = [
    {value:'浙', label:'浙'},
    {value:'京', label:'京'},
    {value:'津', label:'津'},
    {value:'沪', label:'沪'},
    {value:'粤', label:'粤'},
    {value:'渝', label:'渝'},
    {value:'冀', label:'冀'},
    {value:'豫', label:'豫'},
    {value:'云', label:'云'},
    {value:'湘', label:'湘'},
    {value:'鲁', label:'鲁'},
    {value:'苏', label:'苏'},
    {value:'鄂', label:'鄂'},
    {value:'桂', label:'桂'},
    {value:'晋', label:'晋'},
    {value:'陕', label:'陕'},
    {value:'闽', label:'闽'},
    {value:'贵', label:'贵'},
    {value:'皖', label:'皖'},
    {value:'川', label:'川'}
  ];

  beforeSave(): Boolean {
    if(!(this.cardNo && this.vin && this.cardNo2 && this.type && this.check)) {
      ons.notification.toast('表格信息需填写完整!', {timeout: 2000});
      return false;
    }
    if(!RegExp(/^[A-Za-z]/).test(this.cardNo)) {
      ons.notification.toast('车牌号格式不正确!', {timeout: 2000});
      return false;
    }
    // if(this.province === '浙' && RegExp(/^[C|c]/).test(this.cardNo)) {
    //   ons.notification.toast('抱歉, 暂不支持该地区!', {timeout: 2000});
    //   return false;
    // }
    return true;
  }

  async save() {
    if(!this.beforeSave()) return;
    const {success, msg, id} = await this.carService.addCar({
      province: this.province,
      cardNo: this.cardNo,
      cardNo2: this.cardNo2,
      vin: this.vin,
      type: this.type,
      note: this.note
    })
    if(!success) ons.notification.toast(msg, {timeout: 2000});
    else this.route.navigate([`carInfo`], {queryParams: {id}, replaceUrl:true})
  }

  test(e) {
    console.log(e)
    console.log(e.target.value)
    this.province = e.target.value;
  }

  constructor(private route: Router, private carService: CarService) { }

  ngOnInit() {
  }

}
